package com.geeklei.media.ui.widget;

import android.animation.Animator;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.geeklei.media.ui.R;
import com.geeklei.media.ui.common.AnimatorFactory;
import com.geeklei.media.ui.common.Scheduler;

public abstract class GestureView extends FrameLayout {
    private Animator mAnimator = null;
    private Runnable mDissmissRunnable = new Runnable() {
        public void run() {
            GestureView.this.hide();
        }
    };
    protected ImageView mIcon;
    private boolean mIsShowing;
    protected TextView mText;

    protected abstract int getIcon();

    protected abstract int getIconMarginTop();

    protected abstract int getTextMarginTop();

    public GestureView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Context context = getContext();
        this.mIcon = new ImageView(context);
        LayoutParams layoutParams = new LayoutParams(-2, -2);
        layoutParams.topMargin = getIconMarginTop();
        layoutParams.gravity = 17;
        this.mIcon.setLayoutParams(layoutParams);
        this.mIcon.setImageResource(getIcon());
        addView(this.mIcon);
        this.mText = new TextView(context);
        LayoutParams layoutParams2 = new LayoutParams(-2, -2);
        layoutParams2.gravity = 17;
        layoutParams2.topMargin = getTextMarginTop();
        this.mText.setLayoutParams(layoutParams2);
        this.mText.setTextColor(-1);
        this.mText.setTextSize(0, (float) getResources().getDimensionPixelSize(R.dimen.vp_text_size_57));
        addView(this.mText);
        setVisibility(View.GONE);
    }

    public boolean isShowing() {
        return this.mIsShowing;
    }

    public void triggerAutoDismiss() {
        Scheduler.getUIHandler().removeCallbacks(this.mDissmissRunnable);
        Scheduler.getUIHandler().postDelayed(this.mDissmissRunnable, 8000);
    }

    public final void show() {
        if (!this.mIsShowing) {
            animateIn();
            this.mIsShowing = true;
        }
        triggerAutoDismiss();
    }

    public final void hide() {
        if (this.mIsShowing) {
            animateOut();
            this.mIsShowing = false;
        }
    }

    public final void gone() {
        if (this.mIsShowing) {
            setVisibility(View.GONE);
            this.mIsShowing = false;
        }
    }

    public void animateIn() {
        if (this.mAnimator != null) {
            this.mAnimator.end();
        }
        this.mAnimator = AnimatorFactory.animateAlphaIn(this);
    }

    public void animateOut() {
        if (this.mAnimator != null) {
            this.mAnimator.end();
        }
        this.mAnimator = AnimatorFactory.animateAlphaOut(this);
    }
}
